<template>
	<view class="root-wrap">
		<view class="form-item">
			<view class="label">
				<text class="title">其他资质证书</text>
			</view>
			<view class="img-wrap">
				<u-upload
					:fileList="fileList1"
					@afterRead="afterRead"
					@delete="deletePic"
					:previewFullImage="true"
					name="1"
					multiple
					:maxCount="9"
				></u-upload>
			</view>
		</view>
		
		<view class="submit"><u-button @click="onSubmit" color="#30D6C2" type="primary" shape="circle">保存<image class="icon" src="../../../static/assets/imgs/next-step.png"></image></u-button></view>
	</view>
</template>

<script>
	import config from '@/common/config.js';
	import { updateOtherInfo,getUserInfo } from '@/api/user.js';
	export default {
		data() {
			return {
				userInfo: {},
				loading: false,
				readOnly: false,
				fileList1: [],
			};
		},
		onLoad(){
			this.userInfo = this.vuex_user;
			this.getUserInfo();
		},
		methods:{
			getUserInfo(){
				getUserInfo().then((res)=>{
					if (1 == res.code) {
						this.userInfo = res.data;
						this.$u.vuex('vuex_user', res.data)
						if(res.data.other_images&&res.data.other_images.length>0){
							const arr = res.data.other_images.map((item)=>{
								return {
									url: config.exteralImageBaseUrl+item
								}
							})
							this.fileList1 = arr
						}
					} else {
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					console.log(result)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: config.baseUrl+'addons/bjjb/api.common/upload',
						filePath: url,
						name: 'file',
						formData: {
							
						},
						success: (uploadFileRes) => {
							const res = JSON.parse(uploadFileRes.data)
							setTimeout(() => {
								resolve(res.data.fullurl)
							}, 500)
						}
					});
				})
			},
			formatUrl(fileList){
				let image_str = [];
				if(fileList.length>0){
					for(var i = 0;i<fileList.length;i++){
						const url = fileList[i].url;
						const str = url.replace(/^.*?(\/uploads.*)$/, '$1');
						image_str.push(str);
					}
					
				}
				return image_str;
			},
			onSubmit(){
				if(this.loading) return;
				const { id } = this.userInfo;
				this.loading = true;
				uni.showLoading({
					title: '提交中...'
				})
				const params = {
					"vendor_id": id, //供应商ID
					"other_images": this.formatUrl(this.fileList1)
				}
				updateOtherInfo(params).then(res => {
					this.loading = false;
					uni.hideLoading();
					if(res.code==1){
						uni.showToast({
							title:'提交成功',
							icon:'success'
						})
						const timer = setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
							clearTimeout(timer);
						},900);
					}else {
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.root-wrap{
	padding-bottom: 110rpx;
	.form-item{
		margin: 0 24rpx;
		.label{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			margin: 44rpx 0 24rpx;
			.title{
				font-size: 28rpx;
				color: #606266;
				line-height: 50rpx;
			}
			.require{
				font-size: 28rpx;
				color: #E62020;
				line-height: 50rpx;
				margin-left: 24rpx;
			}
		}
		.input-wrap{
			width: 700rpx;
			height: 90rpx;
			background-color: #F3F4F6;
			margin: 0 auto;
			border-radius: 8rpx;
			.u-input{
				height: 100% !important;
				input{
					padding: 0 24rpx !important;
				}
			}
		}
	}
	.submit{
		width: 208rpx;
		height: 80rpx;
		margin: 874rpx auto 110rpx;
		.icon{
			width: 48rpx;
			height: 48rpx;
			margin-left: 16rpx;
		}
		.label{
			font-weight: 500;
			font-size: 32rpx;
			color: #fff;
		}
	}
	.u-input__content__subfix-icon{
		margin-right: 16rpx;
	}
}
</style>
